package com.quick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quick.base.Page;
import com.quick.base.Search;
import com.quick.blog.dto.BlogArticleDTO;
import com.quick.blog.pojo.BlogArticle;
import com.quick.blog.search.BlogArticleSearch;
import com.quick.blog.vo.BlogArticleVo;
import com.quick.constants.Constants;
import com.quick.mapper.ArticleMapper;
import com.quick.service.IArticleService;
import com.quick.util.Convert;
import com.quick.util.LongUtil;
import com.quick.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

@Service
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Override
    public Page<BlogArticleVo> search(Search<BlogArticleSearch> search) {
        PageHelper.startPage(search.getPageNum(), search.getPageSize());
        QueryWrapper<BlogArticle> queryWrapper = new QueryWrapper<BlogArticle>();
        queryWrapper.lambda().orderByDesc(BlogArticle::getUpdateTime);
        List<BlogArticle> lists = articleMapper.selectList(queryWrapper);
		PageInfo<?> pageInfo = new PageInfo(lists);
        return PageUtil.page(pageInfo,BlogArticleVo.class);
    }

    @Override
    public Boolean save(BlogArticleDTO article) {
        BlogArticle entity = Convert.convert(article,BlogArticle.class);
        if(LongUtil.isNull(entity.getId())){
            return articleMapper.updateById(entity)>0;
        }
        return articleMapper.insert(entity)>0;
    }

    @Override
    public BlogArticleVo queryById(BigInteger id) {
        BlogArticle result = articleMapper.selectById(id);
        return Convert.convert(result,BlogArticleVo.class);
    }

    @Override
    public Boolean batchDel(List<Long> ids) {
        return articleMapper.deleteBatchIds(ids)>0;
    }

	@Override
	public List<BlogArticleVo> hots() {
		QueryWrapper<BlogArticle> queryWrapper = new QueryWrapper<BlogArticle>();
		queryWrapper.lambda().eq(BlogArticle::getRecommend, Constants.YES);
        queryWrapper.lambda().orderByDesc(BlogArticle::getUpdateTime);
        List<BlogArticle> lists = articleMapper.selectList(queryWrapper);
		return Convert.convert(lists, BlogArticleVo.class);
	}
}
